package com.yunji.news.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunji.news.entity.Article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 文章Mapper接口
 *
 * @author yunji
 * @since 2024-11-23
 */
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {
    
    /**
     * 分页查询文章列表
     */
    @Select("<script>" +
            "SELECT a.*, c.name as category_name, s.name as school_name " +
            "FROM articles a " +
            "LEFT JOIN categories c ON a.category_id = c.id " +
            "LEFT JOIN schools s ON a.school_id = s.id " +
            "WHERE a.deleted = 0 AND a.status = 1 " +
            "<if test='categoryId != null'>" +
            "AND a.category_id = #{categoryId}" +
            "</if>" +
            "<if test='schoolId != null'>" +
            "AND a.school_id = #{schoolId}" +
            "</if>" +
            "<if test='isTop != null'>" +
            "AND a.is_top = #{isTop}" +
            "</if>" +
            "<if test='isHot != null'>" +
            "AND a.is_hot = #{isHot}" +
            "</if>" +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND (a.title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR a.summary LIKE CONCAT('%', #{keyword}, '%'))" +
            "</if>" +
            "ORDER BY a.is_top DESC, a.publish_time DESC, a.id DESC" +
            "</script>")
    IPage<Article> selectArticlePage(Page<Article> page,
                                     @Param("categoryId") Long categoryId,
                                     @Param("schoolId") Long schoolId,
                                     @Param("isTop") Integer isTop,
                                     @Param("isHot") Integer isHot,
                                     @Param("keyword") String keyword);
    
    /**
     * 获取首页推荐文章
     */
    @Select("SELECT a.*, c.name as category_name FROM articles a " +
            "LEFT JOIN categories c ON a.category_id = c.id " +
            "WHERE a.deleted = 0 AND a.status = 1 AND c.code = #{categoryCode} " +
            "ORDER BY a.is_top DESC, a.publish_time DESC " +
            "LIMIT #{limit}")
    List<Article> selectRecommendedByCategory(@Param("categoryCode") String categoryCode, 
                                              @Param("limit") Integer limit);
    
    /**
     * 增加浏览次数
     */
    @Update("UPDATE articles SET view_count = view_count + 1 WHERE id = #{id}")
    void increaseViewCount(@Param("id") Long id);
    
    /**
     * 获取热门文章
     */
    @Select("SELECT a.*, c.name as category_name FROM articles a " +
            "LEFT JOIN categories c ON a.category_id = c.id " +
            "WHERE a.deleted = 0 AND a.status = 1 " +
            "ORDER BY a.view_count DESC, a.publish_time DESC " +
            "LIMIT #{limit}")
    List<Article> selectHotArticles(@Param("limit") Integer limit);
}